/*
 * File Name：Executor.java
 * Copyright：Copyright 2008-2014 CiWong.Inc. All Rights Reserved.
 * Description： Executor.java
 * Modify By：res-jianpingliu
 * Modify Date：2014-10-30
 * Modify Type：Add
 */
package com.ciwong.mobilelib.utils.executor;

import com.ciwong.libs.utils.CWLog;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 执行器Util
 *
 * @author res-jianpingliu
 * @version ciwong v.1.0 2014-10-30
 * @since ciwong v.1.0
 */
public class BlurExecutor implements Executor
{
    /**
     * Log标识
     */
    public static final String TAG = BlurExecutor.class.getSimpleName();

    private static BlurExecutor mExecutor;

    /**
     * 网络请求执行器
     */
    // 正在执行的任务锁
    private final String taskLock = "executingCount";

    // 最多同时执行的任务数量
    private final int maxExecutingCount = 1;

    /**
     * 网络线程队列
     */
    private LinkedBlockingQueue<Task> taskQueue = new LinkedBlockingQueue<Task>();

    /**
     * 正在执行中的任务
     */
    private List<Task> executingList = new ArrayList<Task>();

    private int executingCount;

    /**
     * 私有构造函数
     */
    private BlurExecutor()
    {
        CWLog.w(TAG, "Create a NetExecutor");
    }

    /**
     * 获取网络请求执行器单例
     *
     * @return NetExecutor
     */
    public static BlurExecutor getExecutor()
    {
        if (mExecutor == null)
        {
            synchronized (BlurExecutor.class)
            {
                if (mExecutor == null)
                {
                    mExecutor = new BlurExecutor();
                }
            }
        }
        return mExecutor;
    }

    @Override
    public boolean addTask(Task task)
    {
        synchronized (taskLock)
        {
            taskQueue.add(task);
            execute();
            return true;
        }
    }

    @Override
    public void cancelTask(Task task)
    {
        synchronized (taskLock)
        {
            taskQueue.remove(task);
        }
    }

    @Override
    public void executeNext(Object task)
    {
        synchronized (taskLock)
        {
            CWLog.i(TAG, "任务执行完毕，下一个>>>");
            if (executingList.contains(task))
            {
                executingList.remove(task);
                if (executingCount > 0)
                {
                    executingCount--;
                }
            }
            execute();
        }
    }

    /**
     * 执行任务
     */
    public void execute()
    {
        if (taskQueue.size() > 0)
        {
            if (executingCount < maxExecutingCount)
            {
                Task task = taskQueue.poll();
                if (task != null)
                {
                    executingList.add(task);
                    executingCount++;
                    task.execute();
                }
            }
        }
        else
        {
            if (executingList.size() == 0 && executingCount == 0
                    && taskQueue.size() == 0)
            {
                return;
            }
        }
    }

    /**
     * 清空所有的任务
     */
    public void clear()
    {
        synchronized (taskLock)
        {
            taskQueue.clear();
            executingList.clear();
            executingCount = 0;
        }
    }

    public static abstract class BlurTask implements Task
    {
        @Override
        public int getPriroy()
        {
            return 0;
        }

        @Override
        public void setTag(Object o)
        {

        }

        @Override
        public Object getTag()
        {
            return null;
        }
    }
}
